package com.ruoyi.caigou.controller;

import com.ruoyi.caigou.service.IBuyBackOrderService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.caigou.BuyBackOrder;
import com.ruoyi.system.api.domain.caigou.BuyBackOrderDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 采购退货单Controller
 *
 * @author wl
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/buyBackOrder")
@Slf4j
public class BuyBackOrderController extends BaseController {
    @Autowired
    private IBuyBackOrderService buyBackOrderService;

    /**
     * 查询采购退货单列表
     */
    @RequiresPermissions("caigou:buyBackOrder:list")
    @GetMapping("/list")
    public TableDataInfo list(BuyBackOrder buyBackOrder) {
        log.info("BuyBackOrderController list buyBackOrder:{}", buyBackOrder);
        startPage();
        List<BuyBackOrder> list = buyBackOrderService.selectBuyBackOrderList(buyBackOrder);
        TableDataInfo tableDataInfo = getDataTable(list);
        log.info("BuyBackOrderController list tableDataInfo:{} list:{}", tableDataInfo, list);
        return tableDataInfo;
    }

    @RequiresPermissions("caigou:buyBackOrder:list")
    @GetMapping("/noReconList")
    public TableDataInfo noReconList(BuyBackOrder buyBackOrder) {
        log.info("BuyBackOrderController noReconList buyBackOrder:{}", buyBackOrder);
        startPage();
        List<BuyBackOrder> list = buyBackOrderService.noReconList(buyBackOrder);
        TableDataInfo tableDataInfo = getDataTable(list);
        log.info("BuyBackOrderController noReconList tableDataInfo:{} list:{}", tableDataInfo, list);
        return tableDataInfo;
    }

    /**
     * @return com.ruoyi.common.core.web.page.TableDataInfo
     * @description 财务收款页面获取采购退货单列表
     * @date 2024/11/12 上午10:52
     * @Param buyBackOrder:
     **/
    @RequiresPermissions("caigou:buyBackOrder:list")
    @GetMapping("/listForReciveOrder")
    public TableDataInfo listForReciveOrder(BuyBackOrder buyBackOrder) {
        log.info("BuyBackOrderController listForReciveOrder buyBackOrder:{}", buyBackOrder);
        startPage();
        List<BuyBackOrder> list = buyBackOrderService.selectBuyBackOrderListForReciveOrder(buyBackOrder);
        TableDataInfo tableDataInfo = getDataTable(list);
        log.info("BuyBackOrderController listForReciveOrder tableDataInfo:{} list:{}", tableDataInfo, list);
        return tableDataInfo;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 查询所有采购退货单
     * @date 2024/11/9 上午11:02
     **/
    @RequiresPermissions("caigou:buyBackOrder:list")
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        log.info("BuyBackOrderController listAll start...");
        BuyBackOrder buyBackOrder = new BuyBackOrder();
        buyBackOrder.setApprovalResult(2);
        buyBackOrder.setOrderStatus(3);
        buyBackOrder.setIsStockOut(0);
        List<BuyBackOrder> list = buyBackOrderService.selectBuyBackOrderList(buyBackOrder);
        log.info("BuyBackOrderController listAll list:{}", list);
        return AjaxResult.success(list);
    }

    /**
     * 导出采购退货单列表
     */
    @RequiresPermissions("caigou:buyBackOrder:export")
    @Log(title = "采购退货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyBackOrder buyBackOrder) {
        List<BuyBackOrder> list = buyBackOrderService.selectBuyBackOrderList(buyBackOrder);
        ExcelUtil<BuyBackOrder> util = new ExcelUtil<BuyBackOrder>(BuyBackOrder.class);
        util.exportExcel(response, list, "采购退货单数据");
    }

    /**
     * 获取采购退货单详细信息
     */
    @RequiresPermissions("caigou:buyBackOrder:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        log.info("BuyBackOrderController getInfo id:{}", id);
        AjaxResult ajaxResult = buyBackOrderService.selectBuyBackOrderById(id);
        log.info("BuyBackOrderController getInfo ajaxResult:{} id:{}", ajaxResult, id);
        return ajaxResult;
    }

    /**
     * 获取当日采购退货笔数及金额
     * @return
     */
    @RequiresPermissions("caigou:buyBackOrder:query")
    @GetMapping(value = "getBuyBackCountAndMoney")
    public AjaxResult getBuyBackCountAndMoney() {
        AjaxResult ajaxResult = success(buyBackOrderService.getBuyBackCountAndMoney());
        log.info("BuyBackOrderController getBuyBackCountAndMoney ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 新增采购退货单
     */
    @RequiresPermissions("caigou:buyBackOrder:add")
    @Log(title = "采购退货单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyBackOrder buyBackOrder) {
        log.info("BuyBackOrderController add buyBackOrder:{}", buyBackOrder);
        AjaxResult ajaxResult = buyBackOrderService.insertBuyBackOrder(buyBackOrder);
        log.info("BuyBackOrderController add ajaxResult:{} buyBackOrder:{}", ajaxResult, buyBackOrder);
        return ajaxResult;
    }

    /**
     * 修改采购退货单
     */
    @RequiresPermissions("caigou:buyBackOrder:edit")
    @Log(title = "采购退货单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyBackOrder buyBackOrder) {
        log.info("BuyBackOrderController edit buyBackOrder:{}", buyBackOrder);
        AjaxResult ajaxResult = buyBackOrderService.updateBuyBackOrder(buyBackOrder);
        log.info("BuyBackOrderController edit ajaxResult:{} buyBackOrder:{}", ajaxResult, buyBackOrder);
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 财务收款单审核通过后修改状态
     * @date 2024/11/12 上午11:28
     * @Param buyBackOrder:
     **/
//    @RequiresPermissions("caigou:buyBackOrder:edit")
    @PostMapping("/update/afterAuditReciveOrder")
    public AjaxResult editAfterAuditReciveOrder(@RequestBody BuyBackOrder buyBackOrder) {
        log.info("BuyBackOrderController editAfterAuditReciveOrder buyBackOrder:{}", buyBackOrder);
        AjaxResult ajaxResult = buyBackOrderService.updateAfterAuditReciveOrder(buyBackOrder);
        log.info("BuyBackOrderController editAfterAuditReciveOrder ajaxResult:{} buyBackOrder:{}", ajaxResult, buyBackOrder);
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 审核后修改状态，生成采购退货出库需求单 采购退货应收单
     * @date 2024/11/7 上午11:49
     * @Param buyBackOrder:
     **/
    @PostMapping("/updateBuyBackOrderStatus")
    public AjaxResult updateBuyBackOrderStatus(@RequestBody BuyBackOrder buyBackOrder) {
        log.info("BuyBackOrderController updateBuyBackOrderStatus buyBackOrder:{}", buyBackOrder);
        AjaxResult ajaxResult = buyBackOrderService.updateBuyBackOrderStatus(buyBackOrder);
        log.info("BuyBackOrderController updateBuyBackOrderStatus ajaxResult:{} buyBackOrder:{}", ajaxResult, buyBackOrder);
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据采购退货单编号修改采购退货单
     * @date 2024/11/9 下午3:02
     * @Param buyBackOrder:
     **/
    @PostMapping("/update/buyBackOrder/byOrderCode")
    public AjaxResult updateBuyBackOrderByOrderCode(@RequestBody BuyBackOrder buyBackOrder) {
        log.info("BuyBackOrderController updateBuyBackOrderByOrderCode buyBackOrder:{}", buyBackOrder);
        AjaxResult ajaxResult = buyBackOrderService.updateBuyBackOrderByOrderCode(buyBackOrder);
        log.info("BuyBackOrderController updateBuyBackOrderByOrderCode ajaxResult:{} buyBackOrder:{}", ajaxResult, buyBackOrder);
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据采购退货单编号获取采购退货单以及详情
     * @date 2024/11/9 下午2:50
     * @Param buyBackOrderCode:
     **/
    @GetMapping("/info/buyBackOrderCode/{buyBackOrderCode}")
    public R<BuyBackOrder> getInfoByBuyBackOrderCode(@PathVariable("buyBackOrderCode") String buyBackOrderCode) {
        log.info("BuyBackOrderController getInfoByBuyBackOrderCode buyBackOrderCode:{}", buyBackOrderCode);
        BuyBackOrder buyBackOrder = buyBackOrderService.selectBuyBackOrderByBuyBackOrderCode(buyBackOrderCode);
        log.info("BuyBackOrderController getInfoByBuyBackOrderCode buyBackOrder:{} buyBackOrderCode:{}", buyBackOrder, buyBackOrderCode);
        return R.ok(buyBackOrder);
    }

    /**
     * 删除采购退货单
     */
    @RequiresPermissions("caigou:buyBackOrder:remove")
    @Log(title = "采购退货单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        log.info("BuyBackOrderController remove ids:{}", Arrays.toString(ids));
        AjaxResult ajaxResult = buyBackOrderService.deleteBuyBackOrderByIds(ids);
        log.info("BuyBackOrderController remove ajaxResult:{} ids:{}", ajaxResult, Arrays.toString(ids));
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 修改采购退货详细
     * @date 2024/11/9 上午10:25
     **/
    @PostMapping("/update/buyBackOrderDetail")
    public AjaxResult updateBuyBackOrderDetail(@RequestBody BuyBackOrderDetail buyBackOrderDetail) {
        log.info("BuyBackOrderController updateBuyBackOrderDetail buyBackOrderDetail:{}", buyBackOrderDetail);
        AjaxResult ajaxResult = buyBackOrderService.updateBuyBackOrderDetail(buyBackOrderDetail);
        log.info("BuyBackOrderController updateBuyBackOrderDetail ajaxResult:{} buyBackOrderDetail:{}", ajaxResult, buyBackOrderDetail);
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据采购退货单id和商品id修改采购退货单详情
     * @date 2024/11/11 下午2:10
     * @Param buyBackOrderDetail:
     **/
    @PostMapping("/update/buyBackOrderDetail/byOrderIdGoodsId")
    public AjaxResult updateBuyBackOrderDetailByOrderIdGoodsId(@RequestBody BuyBackOrderDetail buyBackOrderDetail) {
        log.info("BuyBackOrderController updateBuyBackOrderDetailByOrderIdGoodsId buyBackOrderDetail:{}", buyBackOrderDetail);
        AjaxResult ajaxResult = buyBackOrderService.updateBuyBackOrderDetailByOrderIdGoodsId(buyBackOrderDetail);
        log.info("BuyBackOrderController updateBuyBackOrderDetailByOrderIdGoodsId ajaxResult:{} buyBackOrderDetail:{}", ajaxResult, buyBackOrderDetail);
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.domain.R<com.ruoyi.system.api.domain.caigou.BuyBackOrderDetail>
     * @description 根据采购退货单id和商品id获取采购退货单详情
     * @date 2024/11/14 上午10:47
     * @Param buyBackOrderDetail:
     **/
    @PostMapping("/info/buyBackOrderDetail/byOrderIdGoodsId")
    public R<BuyBackOrderDetail> getBuyBackOrderDetailByOrderIdGoodsId(@RequestBody BuyBackOrderDetail buyBackOrderDetail) {
        log.info("BuyBackOrderController getBuyBackOrderDetailByOrderIdGoodsId buyBackOrderDetail:{}", buyBackOrderDetail);
        BuyBackOrderDetail buyBackOrderDetailR = buyBackOrderService.getBuyBackOrderDetailByOrderIdGoodsId(buyBackOrderDetail);
        log.info("BuyBackOrderController getBuyBackOrderDetailByOrderIdGoodsId buyBackOrderDetailR:{} buyBackOrderDetail:{}", buyBackOrderDetailR, buyBackOrderDetail);
        return R.ok(buyBackOrderDetailR);
    }

    @GetMapping("/check/status/{id}")
    public AjaxResult checkStockPayStatus(@PathVariable("id") Integer id) {
        log.info("BuyBackOrderController checkStockPayStatus id:{}", id);
        AjaxResult ajaxResult = buyBackOrderService.checkStockPayStatus(id);
        log.info("BuyBackOrderController checkStockPayStatus ajaxResult:{} id:{}", ajaxResult, id);
        return ajaxResult;
    }

}
